<?php
session_start();
require_once "include/database.php";
$db = db_connect();
require_once "include/auth.php";
$current_user = current_user();
require_login();
require_user(0);
$title = "Search Results"; 

if($_POST['course_code'] != ""){
	include "_header.php";
	$tutor_query = $db->prepare("SELECT ru.Name, ru.Email_ID
																		FROM tutors_for AS t, student AS s, course_code AS c, regular_user as ru
																		WHERE t.Tutor_Student_ID=s.Student_ID 
																		AND t.Course_Title=
																			(SELECT c.Course_Title FROM course_code AS c WHERE c.Code=:code) AND c.Code = :code 
																		AND ru.Name=
																			(SELECT ru.Name FROM regular_user AS ru WHERE s.Username=ru.Username)");
	$tutor_query->execute(array("code" => $_POST['course_code'])); 
	
	echo "<div class='container'>";
	echo "<table class='table table-bordered table-hover' style='margin:0 auto 0 auto;width:400px;'>";
	echo "<h2 style='text-align:center;'> Search Results for " . $_POST['course_code'] . "</h2>";
	echo "<thead style='background-color:darkgrey'><tr>" .
    "<td style=text-align:center>Tutor Name</td>" .
    "<td style=text-align:center>Tutor Email</td>" .
    "</tr></thead><tbody>"; 
	
	while ($result = $tutor_query->fetch()){
		echo "<tr><td style=text-align:center>" . $result['Name'] . "</td>" . 
		"<td style=text-align:center>" . $result['Email_ID'] . "</td></tr>";
	}

	echo "</tbody></table></div>";




}elseif($_POST['keyword'] != ""){
	include "_header.php";
	
	$keyword_match_query = $db->prepare("SELECT Code, Course_Title FROM course_code WHERE Course_Title LIKE ?");
	$keyword_match_query->execute(array("%$_POST[keyword]%"));

	while($k_result = $keyword_match_query->fetch()){
		$tutor_query = $db->prepare("SELECT ru.Name, ru.Email_ID
																		FROM tutors_for AS t, student AS s, course_code AS c, regular_user as ru
																		WHERE t.Tutor_Student_ID=s.Student_ID 
																		AND t.Course_Title=
																			(SELECT c.Course_Title FROM course_code AS c WHERE c.Code= :code) AND c.Code = :code 
																		AND ru.Name=
																			(SELECT ru.Name FROM regular_user AS ru WHERE s.Username=ru.Username)");
		$tutor_query->execute(array("code" => $k_result['Code'])); 

		echo "<div class='container'>";
		echo "<table class='table table-bordered table-hover' style='margin:0 auto 0 auto;width:400px;'>";
		echo "<h2 style='text-align:center;'> Search Results for " . $k_result['Code'] . " - " . $k_result['Course_Title'] . "</h2>";
		echo "<thead style='background-color:darkgrey'><tr>" .
	    "<td style=text-align:center>Tutor Name</td>" .
	    "<td style=text-align:center>Tutor Email</td>" .
	    "</tr></thead><tbody>"; 
		
		while ($result = $tutor_query->fetch()){
			echo "<tr><td style=text-align:center>" . $result['Name'] . "</td>" . 
			"<td style=text-align:center>" . $result['Email_ID'] . "</td></tr>";
		}

		echo "</tbody></table></div>";
	}


}else{
			header("Location: tutorsearch.php?error=1");
}
?>
<?php include "_footer.php";?>